


#include "main.h"
#include "mir3da.h"



#define VER_TRIGGER_FACTOR 10		// the smaller the value, the more sensitive the trigger
#define ANTI_WAVE_FACTOR 1			// the smaller the value, the stronger the anti-interference
#define TILT_LIMIT_FACTOR 36		// This value is used to limit the trigger when the tilt angle is greater than a certain value. 
									// The larger the value, the greater the tilt angle limit.

#define ACC_Y_TILT_INIT_VALUE 0		//  When it is level, the initial value of the horizontal direction y of the accelerometer
#define ACC_Z_TILT_INIT_VALUE -12		//  When it is level, the initial value of the horizontal direction z of the accelerometer

#define SAMPLE_INTERVAL_CYCLE_MS 10		// Sampling interval, unit: ms
#define LED_ON_TIME_PER_TRIGGER_MS 100	// The LED is on for a period of time after each trigger, unit: ms


void main(void)
{

	signed char da_y_char, da_z_char;					// horizontal two accelerometer data
	signed short da_x;									// vertical accelerometer data note: this is 16-bit data

	signed char diff_x, diff_y_char, diff_z_char;

	signed char da_y_last_char, da_z_last_char;
	signed short da_x_last;

	unsigned char light_status = 0;						// light status, bit 7 is light flag, bit 0-6 is light time

	WDTEN = 0;
	inital(); // �˿ڳ�ʼ��
	mir3da_init();		// Accelerometer initialization
	DelayMs(100); // ��ʱ�ȴ�ϵͳ�����ȶ�

#asm
	CLRWDT
#endasm

	// T0_interrupt_inital(); // Open the period of sleep initialization

	WDTEN = 1;			// Open the watchdog

	while (1)
	{


		#asm
		CLRWDT							// Clear the watchdog count
		#endasm

		// mir3da_read_data(&da_x,&da_y,&da_z);

		mir3da_read_data_char(&da_x,&da_y_char,&da_z_char);		// Read accelerometer data

		#asm
		CLRWDT		// Clear the watchdog count
		#endasm

		

		diff_x = da_x - da_x_last;						// Calculate the difference between the current accelerometer data and the last data in the vertical direction x			
		diff_y_char = da_y_char - da_y_last_char;		// Calculate the difference between the current accelerometer data and the last data in the horizontal direction y
		diff_z_char = da_z_char - da_z_last_char;		// Calculate the difference between the current accelerometer data and the last data in the horizontal direction z


		if(diff_x < 0)					   				// take the absolute value
		{
			diff_x = -diff_x;
		}

		if(diff_y_char < 0)								// take the absolute value
		{
			diff_y_char = -diff_y_char;
		}

		if(diff_z_char < 0)								// take the absolute value
		{
			diff_z_char = -diff_z_char;
		}

		da_x_last = da_x;								// update the last accelerometer data
		da_y_last_char = da_y_char;						// update the last accelerometer data
		da_z_last_char = da_z_char;						// update the last accelerometer data

		if((da_y_char < (ACC_Y_TILT_INIT_VALUE + TILT_LIMIT_FACTOR))
			&& (da_y_char > (ACC_Y_TILT_INIT_VALUE - TILT_LIMIT_FACTOR)))		// judge whether the horizontal direction y tilt range is within the specified range
		{
			if((da_z_char < (ACC_Z_TILT_INIT_VALUE + TILT_LIMIT_FACTOR)) 
				&& (da_z_char > (ACC_Z_TILT_INIT_VALUE - TILT_LIMIT_FACTOR)))	// judge whether the horizontal direction z tilt range is within the specified range
			{

				if((diff_y_char < ANTI_WAVE_FACTOR) && (diff_z_char < ANTI_WAVE_FACTOR))	//  judge whether the horizontal acceleration value is within the specified range to prevent lateral shaking from misfiring
				{
					if(diff_x > VER_TRIGGER_FACTOR)							// vertical trigger condition
					{
						light_status = 0x80 | 0x00;
					}

				}

			}
		}

		if ((light_status >> 7) == 1)
		{
			if((light_status & 0x7F) > (LED_ON_TIME_PER_TRIGGER_MS / SAMPLE_INTERVAL_CYCLE_MS))
			{
				light_status = 0x00;
				LED_OFF;
			}
			else
			{
				light_status++;
				LED_ON;
			}
		}
		else
		{
			LED_OFF;
		}


		#asm
		CLRWDT									// clean the watchdog count
		#endasm

		DelayMs(SAMPLE_INTERVAL_CYCLE_MS);							


// #asm
// 		SLEEP
// #endasm
	}
}


void T0_interrupt_inital(void)
{
	T0CK = 0;	   // T0����ʱ����T0CS����
	OPTION = 0x00; // T0ʱ�ӣ�Fcpu / 2
	T0 = 0X00;	   // T0��ʼֵ
	T0IE = 1;	   // ʹ��T0�ж�
	T0IF = 0;	   // ��T0�жϱ�־
	GIE = 1;	   // ʹ��ȫ���ж�
}

void inital(void)
{
	/* PORTB = 0X00;	//set PORTB as low
	 TRISB = 0X00;	//set PORTB as output
	 PHCON = 0X00;	//set PORTH as low
	 PDCON = 0XFF;	//
	 ODCON = 0X00;*/

	TRISB0 = 0; // set PORTB0 as output
	TRISB1 = 0; // set PORTB1 as output
	PORTB0 = 1; // set PORTB0 as high,SCL
	PORTB1 = 1; // set PORTB1 as high,SDA

	TRISB2 = 0; // set PORTB2 as output
	PORTB2 = 0; // set PORTB2 as low,LED,�͵�ƽ��Ч

	TRISB2 = 0; // set PORTB2 as output
	PORTB2 = 0; // set PORTB2 as low,INT
}


void interrupt Timer0_Isr(void)
{
	if (T0IF) // Judge T0IF
	{
		T0IF = 0;  // Clear T0IF
		T0 = 0x01; // ����T0ֵ,��ʱ5ms,����ֵΪCE��������Ҫ��������������ʱ������Ҫ�Դ�ΪD5
	}
}

void DelayUs(unsigned char t)
{
	while (--t)
		;
}



void DelayMs(unsigned int t)
{
	unsigned char count = 195;
	while (t--)
	{
		while (count--)
			;
	}
}

